package com.mmp.main.servlet.joborder;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.mmp.common.servlet.BasicServlet;
import com.mmp.main.entity.SpInventoryContent;
import com.mmp.main.util.WebUtil;

public class JobOrderSPInventoryAjax extends BasicServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException {

		

		String code = WebUtil.getParamValue(request, "code", null);
		Integer quantity = WebUtil.getParamValueAsInteger(request, "quantity", null);

		PrintWriter out = response.getWriter();
		JSONObject jsonObj = new JSONObject();
		
		if (code == null || code.isEmpty()) {
			//jsonObj.put("msg", "يجب تحديد الكود المطلوب أولا");
			out.print(true);
			//out.print(jsonObj);
			return;
		}

		SpInventoryContent spInventoryContent = dbQueryManager.getSpInventoryContentWithStats(
				getSessionSiteId(request), code);

		if (spInventoryContent == null) {
			jsonObj.put("msg", "الكود المطلوب غير موجود");
			out.print(jsonObj);
		} else {
			if (quantity == null) {
				jsonObj.put("spDescription", spInventoryContent.getName());
				jsonObj.put("spQuantity", spInventoryContent.getAvailableQuantity());
				jsonObj.put("spPrice", spInventoryContent.getAvgUnitPrice());
				out.print(jsonObj);
			} else if (quantity > spInventoryContent.getAvailableQuantity())
				out.print(false);
			else
				out.print(true);
		}
	}
}
